home *** CD-ROM | disk | FTP | other *** search
- /*
- * ⌐ Graeme Gerrard 1990
- * Faculty of Music, University of Melbourne
- * Parkville Victoria 3052 Australia.
- *
- * ARPANET: grae@murdu.ucs.unimelb.edu.au
- * telephone: (613) 344 4127, Fax: (613) 344 5346
- */
- #include "Music4C.h"
- #include <unix.h>
- #include <SANE.h>
- #include "AIFFtype.h"
- #include "Music4C_Prototype.h"
- #include "ErrorAlert.h"
-
- extern OSErr theErr;
- void prepareAIFFfile(double, int);
- void FixHeaderInfo(void);
-
-
-
- void prepareAIFFfile(TotalDuration, nchnls)
- double TotalDuration;
- int nchnls;
- {
- register i;
- double x;
- Str255 myStr255;
- long aLong;
- extern double srate;
- int resid;
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- MarkerChunk MarkerChunkHeader, *MarkerChunkHeaderPtr;
- InstrumentChunk InstHeader, *InstHeaderPtr;
- long TotalSamps;
- extern Boolean AIFFoddByte;
- extern double srate; /* sampling rate */
- extern Str255 SoundFileName;
- extern ioParam myIOParmBlk;
- extern long SampleRate;
- extern Str255 theMess1;
-
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
- aLong = (long)(TotalDuration * nchnls * srate);
- resid = (int)(aLong % 512L);
- if ( resid != 0 ) {
- aLong += (long)(512 - resid) + 512L;;
- }
- TotalSamps = aLong;
-
- /* initialize Form Chunk info */
- FormChunkHeader.ckID = 'FORM';
- FormChunkHeader.formType = 'AIFF';
-
-
- /* initialize Common Chunk info */
- CommonHeader.ckID = 'COMM';
- CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
- sizeof( CommonHeader.numSampleFrames ) +
- sizeof( CommonHeader.sampleSize ) +
- sizeof( CommonHeader.sampleRate );
- CommonHeader.numChannels = (short)nchnls;
- CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / nchnls);
- CommonHeader.sampleSize = SixteenBits;
-
- SampleRate = (long)srate;
- NumToString(SampleRate, &myStr255);
- CommonHeader.sampleRate = str2num(&myStr255); /* string to extended */
-
-
- SoundDataHeader.ckID = 'SSND';
- SoundDataHeader.offset = 0L;
- SoundDataHeader.blockSize = 0L;
-
- SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
- sizeof( SoundDataHeader.blockSize ) +
- (long)(TotalSamps * sizeof(int));
-
-
-
- FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
-
- FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) +
- sizeof(CommonHeader.ckSize)
- + CommonHeader.ckSize);
-
- FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) +
- sizeof(SoundDataHeader.ckSize)
- + SoundDataHeader.ckSize);
-
- if ( FormChunkHeader.ckSize % 2 != 0 )
- AIFFoddByte = TRUE;
- else
- AIFFoddByte = FALSE;
-
-
- /* write out header info */
- myIOParmBlk.ioReqCount = sizeof(FormChunkHeader);
- myIOParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to sample file");
- OSError(theMess1, NIL, NIL);
-
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to sample file");
- OSError(theMess1, NIL, NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(SoundDataHeader);
- myIOParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to file");
- OSError(theMess1, NIL, NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
- }
-
-
- void FixHeaderInfo()
- {
- /* this is a kludgey way to ensure that the AIFF file header
- * info is accurate. We just read it all back again from the file
- * and patch it up.
- */
-
- register i;
- Str255 myStr255;
- double x;
- extern double srate;
- long aLong;
- int resid;
- Chunk FormChunkHeader, *FormChunkHeaderPtr;
- CommonChunk CommonHeader, *CommonHeaderPtr;
- SoundDataChunk SoundDataHeader, *SoundDataHeaderPtr;
- MarkerChunk MarkerChunkHeader, *MarkerChunkHeaderPtr;
- InstrumentChunk InstHeader, *InstHeaderPtr;
- extern long TotalSamps;
- extern Boolean AIFFoddByte;
- extern double srate; /* sampling rate */
- extern Str255 SoundFileName;
- extern ioParam myIOParmBlk;
- extern int nchnls;
-
- FormChunkHeaderPtr = &FormChunkHeader;
- CommonHeaderPtr = &CommonHeader;
- SoundDataHeaderPtr = &SoundDataHeader;
-
-
- /* reset file position to start */
-
- myIOParmBlk.ioPosMode = fsFromStart;
- myIOParmBlk.ioPosOffset = 0L;
- theErr = PBSetFPos(&myIOParmBlk, FALSE);
- myIOParmBlk.ioPosMode = fsAtMark;
-
-
-
-
- /* initialize Form Chunk info */
- FormChunkHeader.ckID = 'FORM';
- FormChunkHeader.formType = 'AIFF';
-
-
- /* initialize Common Chunk info */
- CommonHeader.ckID = 'COMM';
- CommonHeader.ckSize = sizeof( CommonHeader.numChannels ) +
- sizeof( CommonHeader.numSampleFrames ) +
- sizeof( CommonHeader.sampleSize ) +
- sizeof( CommonHeader.sampleRate );
- CommonHeader.numChannels = (short)nchnls;
- CommonHeader.numSampleFrames = (unsigned long)(TotalSamps / nchnls);
- CommonHeader.sampleSize = SixteenBits;
-
-
- SampleRate = (long)srate;
- NumToString(SampleRate, &myStr255);
- CommonHeader.sampleRate = str2num(&myStr255); /* string to extended */
-
-
-
- SoundDataHeader.ckID = 'SSND';
- SoundDataHeader.offset = 0L;
- SoundDataHeader.blockSize = 0L;
-
- SoundDataHeader.ckSize = sizeof( SoundDataHeader.offset ) +
- sizeof( SoundDataHeader.blockSize ) +
- (long)(TotalSamps * sizeof(int));
-
-
-
- FormChunkHeader.ckSize = sizeof(FormChunkHeader.formType);
-
- FormChunkHeader.ckSize += (sizeof(CommonHeader.ckID) +
- sizeof(CommonHeader.ckSize)
- + CommonHeader.ckSize);
-
- FormChunkHeader.ckSize += (sizeof(SoundDataHeader.ckID) +
- sizeof(SoundDataHeader.ckSize)
- + SoundDataHeader.ckSize);
-
- if ( FormChunkHeader.ckSize % 2 != 0 )
- AIFFoddByte = TRUE;
- else
- AIFFoddByte = FALSE;
-
-
- /* REwrite out header info */
- myIOParmBlk.ioReqCount = sizeof(FormChunkHeader);
- myIOParmBlk.ioBuffer = (Ptr)FormChunkHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to sample file");
- OSError(theMess1, NIL, NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(CommonHeader);
- myIOParmBlk.ioBuffer = (Ptr)CommonHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to sample file");
- OSError(theMess1, NIL, NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
-
- myIOParmBlk.ioReqCount = sizeof(SoundDataHeader);
- myIOParmBlk.ioBuffer = (Ptr)SoundDataHeaderPtr;
- if ( (theErr = PBWrite(&myIOParmBlk, FALSE)) != noErr ) {
- PstringCopy((char *)theMess1, "\pError writing Header to sample file");
- OSError(theMess1, NIL, NIL);
- }
- if ( myIOParmBlk.ioActCount != myIOParmBlk.ioReqCount) {
- PstringCopy((char *)theMess1, "\pError writing Header to file, wrote wrong number of bytes");
- OSError(theMess1, NIL, NIL);
- }
- }
-